// pages/search/searchList.js
import { news, info } from '../../API/index.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 'news', // 默认显示新闻标签
    searchValue: '', // 搜索框内容
    searchResultNews: [
			{
				"newsId": "n001",
				"title": "关于开设外卖柜决定",
				"publisher": "YM",
				"pub_date": "number",
				"coverImgUrl": "string"
			},
			{
				"newsId": "n001",
				"title": "新规章：禁止用洗衣机洗内裤和袜子",
				"publisher": "YM",
				"pub_date": "number",
				"coverImgUrl": "string"
			},
			{
				"newsId": "n001",
				"title": "新规章：禁止用洗衣机洗内裤和袜子",
				"publisher": "YM",
				"pub_date": "number",
				"coverImgUrl": "string"
			},
			{
				"newsId": "n001",
				"title": "新规章：禁止用洗衣机洗内裤和袜子",
				"publisher": "YM",
				"pub_date": "number",
				"coverImgUrl": "string"
			},
			{
				"newsId": "n001",
				"title": "新规章：禁止用洗衣机洗内裤和袜子",
				"publisher": "YM",
				"pub_date": "number",
				"coverImgUrl": "string"
			},
			{
				"newsId": "n001",
				"title": "新规章：禁止用洗衣机洗内裤和袜子",
				"publisher": "YM",
				"pub_date": "number",
				"coverImgUrl": "string"
			},
			{
				"newsId": "n001",
				"title": "新规章：禁止用洗衣机洗内裤和袜子",
				"publisher": "YM",
				"pub_date": "number",
				"coverImgUrl": "string"
			},
			{
				"newsId": "n001",
				"title": "新规章：禁止用洗衣机洗内裤和袜子",
				"publisher": "YM",
				"pub_date": "number",
				"coverImgUrl": "string"
			},
			{
				"newsId": "n001",
				"title": "新规章：禁止用洗衣机洗内裤和袜子",
				"publisher": "YM",
				"pub_date": "number",
				"coverImgUrl": "string"
			},
			{
				"newsId": "n001",
				"title": "新规章：禁止用洗衣机洗内裤和袜子",
				"publisher": "YM",
				"pub_date": "number",
				"coverImgUrl": "string"
			},
			{
				"newsId": "n001",
				"title": "新规章：禁止用洗衣机洗内裤和袜子",
				"publisher": "YM",
				"pub_date": "number",
				"coverImgUrl": "string"
			},
			{
				"newsId": "n001",
				"title": "新规章：禁止用洗衣机洗内裤和袜子",
				"publisher": "YM",
				"pub_date": "number",
				"coverImgUrl": "string"
			},
			{
				"newsId": "n001",
				"title": "新规章：禁止用洗衣机洗内裤和袜子",
				"publisher": "YM",
				"pub_date": "number",
				"coverImgUrl": "string"
			},
			{
				"newsId": "n001",
				"title": "新规章：禁止用洗衣机洗内裤和袜子",
				"publisher": "YM",
				"pub_date": "number",
				"coverImgUrl": "string"
			},
			{
				"newsId": "n001",
				"title": "新规章：禁止用洗衣机洗内裤和袜子",
				"publisher": "YM",
				"pub_date": "number",
				"coverImgUrl": "string"
			},
		], // 新闻搜索结果
    searchResultInfo: [], // 通知公告搜索结果
    loading: false, // 是否正在加载
    pageNumNews: 1, // 新闻当前页码
    pageNumInfo: 1, // 通知当前页码
    hasMoreNews: true, // 新闻是否有更多数据
    hasMoreInfo: true // 通知是否有更多数据
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 从路由参数中获取搜索关键词
    if (options.keyword) {
      this.setData({
        searchValue: decodeURIComponent(options.keyword)
      });
      // 执行搜索
      // this.handleSearch();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  /**
   * 处理输入框内容变化
   */
  onInputChange(e) {
    this.setData({
      searchValue: e.detail.value
    });
  },

  /**
   * 处理搜索按钮点击
   */
  handleSearch() {
    if (this.data.searchValue.trim()) {
      // 重置页码和数据
      this.setData({
        searchResultNews: [],
        searchResultInfo: [],
        pageNumNews: 1,
        pageNumInfo: 1,
        hasMoreNews: true,
        hasMoreInfo: true
      });
      
      // 根据当前标签获取数据
      if (this.data.currentTab === 'news') {
        this.getNewsList();
      } else {
        this.getInfoList();
      }
    }
  },

  /**
   * 切换标签
   */
  switchTab(e) {
    const tab = e.currentTarget.dataset.tab;
    this.setData({
      currentTab: tab
    });
    
    // 如果切换到的标签没有数据，则获取数据
    if (tab === 'news' && this.data.searchResultNews.length === 0) {
      this.getNewsList();
    } else if (tab === 'info' && this.data.searchResultInfo.length === 0) {
      this.getInfoList();
    }
  },
  
  /**
   * 获取新闻列表
   */
  // getNewsList() {
  //   if (this.data.loading || !this.data.hasMoreNews) return;
    
  //   this.setData({ loading: true });
    
  //   news.getNewsListAPI({
  //     keyword: this.data.searchValue,
  //     pageNum: this.data.pageNumNews,
  //     pageSize: 10
  //   }).then(res => {
  //     const newData = res.data || [];
  //     const combinedData = [...this.data.searchResultNews, ...newData];
      
  //     this.setData({
  //       searchResultNews: combinedData,
  //       loading: false,
  //       hasMoreNews: newData.length === 10,
  //       pageNumNews: this.data.pageNumNews + 1
  //     });
  //   }).catch(err => {
  //     console.error('获取新闻列表失败:', err);
  //     this.setData({ loading: false });
  //   });
  // },
  
  /**
   * 获取通知列表
   */
  // getInfoList() {
  //   if (this.data.loading || !this.data.hasMoreInfo) return;
    
  //   this.setData({ loading: true });
    
  //   info.getInfoListAPI({
  //     keyword: this.data.searchValue,
  //     pageNum: this.data.pageNumInfo,
  //     pageSize: 10
  //   }).then(res => {
  //     const newData = res.data || [];
  //     const combinedData = [...this.data.searchResultInfo, ...newData];
      
  //     this.setData({
  //       searchResultInfo: combinedData,
  //       loading: false,
  //       hasMoreInfo: newData.length === 10,
  //       pageNumInfo: this.data.pageNumInfo + 1
  //     });
  //   }).catch(err => {
  //     console.error('获取通知列表失败:', err);
  //     this.setData({ loading: false });
  //   });
  // },
  
  /**
   * 加载更多数据
   */
  loadMore() {
    if (this.data.currentTab === 'news') {
      this.getNewsList();
    } else {
      this.getInfoList();
    }
	},
	
	navBack() {
		wx.navigateBack()
	}
})